'use strict';

const Controller = require('egg').Controller;
class AdminController extends Controller {
  async selectAll() {
    const { ctx } = this;
    const list = await ctx.service.adminService.selectAll();
    ctx.body = { list };
  }

  async register() {
    const { ctx } = this;
    let data = '';
    const adminName =  ctx.request.body.adminName;
    const adminPwd =  ctx.request.body.adminPwd;
    const adminPortrait =  ctx.request.body.adminPortrait;
    const adminSex = ctx.request.body.adminSex;
    const realName = ctx.request.body.realName;
    const nickname = ctx.request.body.nickname;
    const adminPhone = ctx.request.body.adminPhone;
    const adminAge = ctx.request.body.adminAge;
    // const adminPortrait = ctx.request.adminPortrait;
    if (!adminName) {
      ctx.body = { coed: 501, msg: '缺少必填项;密码不能为空' };
    } else if (!adminPwd) {
      ctx.body = { coed: 501, msg: '缺少必填项;密码不能为空' };
    } else {
      data = await ctx.service.adminService.register({
        adminName,
        adminPwd,
        adminPortrait,
        adminSex,
        realName,
        nickname,
        adminPhone,
        adminAge,
      });
      ctx.body = { code: 200, data };
    }
  }

  async login() {
    const { ctx } = this;
    const adminName =  ctx.request.body.adminName;
    const adminPwd =  ctx.request.body.adminPwd;
    if (!adminName) {
      ctx.body = { coed: 501, msg: '用户名不能为空' };
    } else if (!adminPwd) {
      ctx.body = { coed: 501, msg: '密码不能为空' };
    } else {
      const data = await ctx.service.adminService.login({
        adminName,
        adminPwd,
      });
      ctx.body = { code: 200, data };
    }
  }

  async update() {
    const { ctx } = this;
    let data = '';
    const adminName =  ctx.request.body.adminName;
    const adminPwd =  ctx.request.body.adminPwd;
    const adminPortrait =  ctx.request.body.adminPortrait;
    const adminSex = ctx.request.body.adminSex;
    const realName = ctx.request.body.realName;
    const nickname = ctx.request.body.nickname;
    const adminPhone = ctx.request.body.adminPhone;
    const adminAge = ctx.request.body.adminAge;
    // const adminPortrait = ctx.request.adminPortrait;
    if (!adminName) {
      ctx.body = { coed: 501, msg: '缺少必填字段;请输入管理员账号字段!' };
    } else {
      data = await ctx.service.adminService.update({
        adminName,
        adminPwd,
        adminPortrait,
        adminSex,
        realName,
        nickname,
        adminAge,
        adminPhone,
      });
      ctx.body = { code: 200, data };
    }
  }
  async findByAdminName() {
    const { ctx } = this;
    // const adminName = ctx.query.adminName;
    const adminName = ctx.request.body.adminName;
    console.log(adminName);
    if (!adminName) {
      ctx.body = { coed: 501, msg: '缺少必填字段;请输入管理员账号字段!' };
    } else {
      const data = await ctx.service.adminService.findByAdminName(adminName);
      ctx.body = { coed: 200, data };
    }
  }
}
module.exports = AdminController;
